package com.it.lmao.java8.lambda;

import org.junit.jupiter.api.Test;

import java.util.concurrent.Callable;
import java.util.function.BiFunction;
import java.util.function.Function;


/**
 * 
 * @author lmao-pc
 * 	https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.27
 * 
 *         () -> {} // 无参，无返回值 
 *         () -> {} // 无参，无返回值 
 *         () -> System.out.println(1)  // 无参，无返回值（上面的简写）
 *         () -> { return 100; } // 无参，有返回值 
 *         () -> { return 100; } // 无参，有返回值
 *         () -> null // 无参，有返回值（返回null） 
 *         (int x) -> { return x+1; } // 单个参数，有返回值
 *         (int x) -> x+1 // 单个参数，有返回值（上面的简写） 
 *         (x) -> x+1 //单个参数，有返回值（不指定参数类型，多个参数必须用括号） 
 *         x -> x+1 // 单个参数，有返回值（不指定参数类型） 
 *         (x, int y) -> x+y (x, int y) -> x+y 
 *         Object obj = () -> “lmao”
 *
 * 
 * 
 * 
 * 
 * 
 * 重要的2点
 * 	
 * 	参数
 * 	返回值    
 * 
 * 
 */
public class Listen1 {
	/**
	 * 有参数有返回值
	 */
	@Test
	public void test3() {
		IUserService userService=new IUserService() {
			@Override
			public User findByUser(User user) {
				// TODO Auto-generated method stub
				return user;
			}
		};
		
		IUserService userService1=user->user;
		IUserService userService2=(user)->{return user;};
		IUserService userService3=(User user)->{
			System.out.println("user:"+user.toString());
			user.setName("李四");
			return user;
		};
		System.out.println(userService.findByUser(new User("张三", 25)));
		System.out.println(userService1.findByUser(new User("张三", 25)));
		System.out.println(userService2.findByUser(new User("张三", 25)));
		System.out.println(userService3.findByUser(new User("张三", 25)));
		
		
		Function<Integer, Integer > fun=a->{
			int i=0;
			for (int j = 1; j < a; j++) {
				System.out.println(j);
				i+=j;
			}
			return i;
		};
		System.out.println(fun.apply(100));
		
		
		// BiFunction   2个输入 一个输出
		// 重要的2点   参数类型和返回值   -> 左边 相当于参数列表    右边相当与执行的方法体    无论怎么写  返回类型必须一致
		BiFunction<String, String, String> bf = (a, b) -> a + b;
		BiFunction<String, String, String> bf1 = (a, b) -> "wangwu";
		BiFunction<String, String, String> bf2 = (a, b) -> {
			return "二娃子";
		};
		System.out.println(bf.apply("mao", "li"));
		System.out.println(bf1.apply("mao", "li"));
		System.out.println(bf2.apply("mao", "li"));
	}
	
	/*
	 * 无参数 有返回值
	 */
	@Test
	public void test2() throws Exception {
		Callable<String> call=new Callable<String>() {
			@Override
			public String call() throws Exception {
				// TODO Auto-generated method stub
				return "lmao";
			}
		};
		Callable<String> call1=()->"lmao1";
		Callable<String> call2=()->{return "lmao2";};
		System.out.println(call.call());
		System.out.println(call1.call());
		System.out.println(call2.call());
		
	}
	
	/**
	 * 无参数 无返回值
	 */
	@Test
	public void test1() {
		
		Runnable run =new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				System.out.println("1");
			}
		};
		run.run();
		Runnable run1 =()-> System.out.println("go1");
		Runnable run2 =()-> {System.out.println("go2");};
		run1.run();
		run2.run();
		
	}

}

interface IUserService{
    User	findByUser(User user);
	
}

class User {
	private String name;
	private int age;

	public User() {
		// TODO Auto-generated constructor stub
	}

	public User(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + "]";
	}

}